package com.zyx.nettypro.nio;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author zyx
 * @since 2021/9/19 08:31
 */
public class FileChannelWrite {
    public static void main(String[] args) throws Exception {
        String str = "你好， 中国";
        // 创建一个输出流 ==> channel
        FileOutputStream fileOutputStream = new FileOutputStream("input/netty/file01.txt");

        //这个 fileChannel 真实 类型是  FileChannelImpl
        FileChannel fileChannel = fileOutputStream.getChannel();

        //创建一个缓冲区 ByteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        //将 str 放入 byteBuffer
        byteBuffer.put(str.getBytes());

        //对byteBuffer 进行flip
        byteBuffer.flip();

        //将byteBuffer 数据写入到 fileChannel
        fileChannel.write(byteBuffer);
        fileOutputStream.close();

    }
}
